//
//  EmailChecker.m
//  
//
//  Created by Leafc on 13年3月25日.
//  Copyright (c) 2013年 Leafc. All rights reserved.
//

#import "TextChecker.h"

@interface TextChecker ()

+ (BOOL)checkWithPattern:(NSString *)pattern text:(NSString *)text;

@end

@implementation TextChecker

#pragma mark - Private

+ (BOOL)checkWithPattern:(NSString *)pattern text:(NSString *)text
{
    if (!text) {
        return NO;
    }
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
    NSTextCheckingResult *match = [regex firstMatchInString:text options:0 range:NSMakeRange(0, [text length])];
    return (BOOL)match;
}

+ (BOOL)isEmailAddress:(NSString *)text
{
    NSString *pattern = @"^\\b([a-zA-Z0-9%_.+\\-]+)@([a-zA-Z0-9.\\-]+?\\.[a-zA-Z]{2,6})\\b$";
    return [TextChecker checkWithPattern:pattern text:text];
}

+ (BOOL)isTelephone:(NSString *)text
{
    NSString *pattern = @"((\\d{11})|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)";
    return [TextChecker checkWithPattern:pattern text:text];
}

+ (BOOL)isNumeric:(NSString *)text
{
    NSString *pattern = @"^[1-9]\\d*$";
    return [TextChecker checkWithPattern:pattern text:text];
}

+ (BOOL)isQQ:(NSString *)text
{
    NSString *pattern = @"^[1-9][0-9]{4,}$";
    return [TextChecker checkWithPattern:pattern text:text];
}

+ (BOOL)isMSN:(NSString *)text
{
    NSString *pattern = @"^\\w+@hotmail\\.com$";
    return [TextChecker checkWithPattern:pattern text:text];
}

+ (BOOL)isEmptyOrNil:(NSString *)text
{
    BOOL b = (text && text.length > 0);
    return b;
}

@end
